package org.watchrecords.dbo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.StringUtils;
import org.watchrecords.constants.Gender;
import org.watchrecords.constants.OffenderStatus;
import org.watchrecords.constants.Race;

@Entity
@NamedQueries({
@NamedQuery(name = "getOffenders", query = "SELECT o FROM Offender o ORDER BY o.firstName, o.nickname, o.lastName")
})
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"firstName", "lastName", "nickname"}))
public class Offender implements Identifiable {
    /**
     * 
     */
    private static final long serialVersionUID = 1453549461887965046L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    private String firstName;
    private String lastName;
    private String nickname;
    private Date created;
    private Date updated;
    private Integer argentNode;
    
    private Race race;
    private Gender gender;

    @Column(length = 2000)
    private String description;
    @Column(length = 2000)
    private String possessions;
    
    @NotNull(message = "Offender status cannot be empty")
    private OffenderStatus status;
    
    private int unpaidFine;
    
    @OneToMany(mappedBy = "offender", fetch = FetchType.LAZY)
    private List<CellRecord> cellRecords = new ArrayList<CellRecord>();
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public Integer getArgentNode() {
        return argentNode;
    }

    public void setArgentNode(Integer argentNode) {
        this.argentNode = argentNode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPossessions() {
        return possessions;
    }

    public void setPossessions(String possessions) {
        this.possessions = possessions;
    }

    public OffenderStatus getStatus() {
        return status;
    }

    public void setStatus(OffenderStatus status) {
        this.status = status;
    }
    
    public int getUnpaidFine() {
        return unpaidFine;
    }

    public void setUnpaidFine(int unpaidFine) {
        this.unpaidFine = unpaidFine;
    }

    public String getFullName() {
        if (StringUtils.isEmpty(nickname)) {
            return (StringUtils.isEmpty(firstName) ? "" : firstName + " ") + StringUtils.defaultString(lastName);
        }
        else {
            return (StringUtils.isEmpty(firstName) ? "" : firstName + " ") + '"' + nickname + '"' +
                    (StringUtils.isEmpty(lastName) ? "" : " " + lastName);
        }
    }

    public List<CellRecord> getCellRecords() {
        return cellRecords;
    }

    public void setCellRecords(List<CellRecord> cellRecords) {
        this.cellRecords = cellRecords;
    }
    
    public String toString() {
        return getFullName();
    }

    public Race getRace() {
        return race;
    }

    public void setRace(Race race) {
        this.race = race;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }
}
